{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 354,
   "metadata": {},
   "outputs": [],
   "source": [
    "class BinaryNode:\n",
    "    indent = \"  \"\n",
    "\n",
    "    def __init__(self, value) -> None:\n",
    "        self.value = value\n",
    "        self.left_child = None\n",
    "        self.right_child = None\n",
    "\n",
    "    def add_left(self, new_node):\n",
    "        self.left_child = new_node\n",
    "\n",
    "    def add_right(self, new_node):\n",
    "        self.right_child = new_node\n",
    "\n",
    "    def __str__(self, level = 0):\n",
    "        result = f'{self.value}:\\n'\n",
    "        level += 1\n",
    "        \n",
    "        if (self.left_child == None and self.right_child == None):\n",
    "            return result\n",
    "\n",
    "        if (self.left_child == None):\n",
    "            result += f'{self.indent * level}None\\n'\n",
    "        else:\n",
    "            result += f'{self.indent * level}{self.left_child.__str__(level)}'\n",
    "\n",
    "        if (self.right_child == None):\n",
    "            result += f'{self.indent * level}None\\n'\n",
    "        else:\n",
    "            result += f'{self.indent * level}{self.right_child.__str__(level)}'\n",
    "            \n",
    "        return result\n",
    "\n",
    "    # Exhaustive Search\n",
    "    def find_node(self, target):\n",
    "        # If this node contains the target value, return self.\n",
    "        if self.value == target:\n",
    "            return self\n",
    "\n",
    "        # If the left child is not None:\n",
    "        if self.left_child != None:\n",
    "            result = self.left_child.find_node(target)\n",
    "            if result != None:\n",
    "                return result            \n",
    "\n",
    "        # If the right child is not None:\n",
    "        if self.right_child != None:\n",
    "            result = self.right_child.find_node(target)\n",
    "            if result != None:\n",
    "                return result \n",
    "        \n",
    "        return None\n",
    "    \n",
    "    def traverse_preorder(self):\n",
    "        result_list = []\n",
    "        result_list.append(self)\n",
    "        if self.left_child != None:\n",
    "            result_list += self.left_child.traverse_preorder()\n",
    "        if self.right_child != None:\n",
    "            result_list += self.right_child.traverse_preorder()\n",
    "        return result_list\n",
    "    \n",
    "    def traverse_inorder(self):\n",
    "        result_list = []\n",
    "        if self.left_child != None:\n",
    "            result_list += self.left_child.traverse_inorder()\n",
    "        result_list.append(self)\n",
    "        if self.right_child != None:\n",
    "            result_list += self.right_child.traverse_inorder()\n",
    "        return result_list\n",
    "    \n",
    "    def traverse_postorder(self):\n",
    "        result_list = []\n",
    "        if self.left_child != None:\n",
    "            result_list += self.left_child.traverse_postorder()\n",
    "        if self.right_child != None:\n",
    "            result_list += self.right_child.traverse_postorder()\n",
    "        result_list.append(self)\n",
    "        return result_list\n",
    "        \n",
    "    \n",
    "    def traverse_breadth_first(self):\n",
    "        result_list = []\n",
    "        queue_list = []\n",
    "        queue_list.append(self)\n",
    "        while len(queue_list) > 0:\n",
    "            node = queue_list.pop(0)\n",
    "            result_list.append(node)\n",
    "            if node.left_child != None:    \n",
    "                queue_list.append(node.left_child)\n",
    "            if node.right_child != None:    \n",
    "                queue_list.append(node.right_child)\n",
    "        return result_list\n",
    "        "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The breadth-first traversal method works very differently from the others because it is nonrecursive:\n",
    "\n",
    "```\n",
    "# Create an empty list named result.\n",
    "\n",
    "# Create an empty list named queue.\n",
    "# This is a list of nodes waiting their turn to be processed.\n",
    "# Initially place self in queue.\n",
    "\n",
    "# While queue is not empty:\n",
    "    # Remove the first node from queue and add it to result.\n",
    "\n",
    "    # Add the node’s children to the end of queue.\n",
    "\n",
    "# Return result\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 355,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Build a test tree.\n",
    "#      Root\n",
    "#      /  \\\n",
    "#     A    B\n",
    "#    / \\    \\\n",
    "#   C   D    E\n",
    "#           /\n",
    "#          F\n",
    "root_node = BinaryNode(\"Root\")\n",
    "a_node = BinaryNode(\"A\")\n",
    "b_node = BinaryNode(\"B\")\n",
    "c_node = BinaryNode(\"C\")\n",
    "d_node = BinaryNode(\"D\")\n",
    "e_node = BinaryNode(\"E\")\n",
    "f_node = BinaryNode(\"F\")\n",
    "\n",
    "root_node.add_left(a_node)\n",
    "root_node.add_right(b_node)\n",
    "a_node.add_left(c_node)\n",
    "a_node.add_right(d_node)\n",
    "b_node.add_right(e_node)\n",
    "e_node.add_left(f_node)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 356,
   "metadata": {},
   "outputs": [],
   "source": [
    "def find_value(root, target):\n",
    "    node = root.find_node(target)\n",
    "    if node == None:\n",
    "        print(f'Value {target} not found')\n",
    "    else:\n",
    "        print(f'Found {node.value}')\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 357,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Preorder:      Root A C D B E F \n",
      "Inorder:       C A D Root B F E \n",
      "Postorder:     C D A F E B Root \n",
      "Breadth-First: Root A B C D E F \n"
     ]
    }
   ],
   "source": [
    "print('Preorder:      ', end='')\n",
    "for node in root_node.traverse_preorder():\n",
    "    print(f'{node.value} ', end='')\n",
    "print()\n",
    "\n",
    "print('Inorder:       ', end='')\n",
    "for node in root_node.traverse_inorder():\n",
    "    print(f'{node.value} ', end='')\n",
    "print()\n",
    "\n",
    "print('Postorder:     ', end='')\n",
    "for node in root_node.traverse_postorder():\n",
    "    print(f'{node.value} ', end='')\n",
    "print()\n",
    "\n",
    "print('Breadth-First: ', end='')\n",
    "for node in root_node.traverse_breadth_first():\n",
    "    print(f'{node.value} ', end='')\n",
    "print()\n"
   ]
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "e63faaf9bc27d837377622cad56f3d439b9a97e8fa476559128293c868ce68e0"
  },
  "kernelspec": {
   "display_name": "Python 3.9.7 64-bit ('word_database': conda)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
